package online.hxstart.code.easy;

import java.util.Arrays;

/**
 * @author huxing
 * @date 2018-12-10 15:34
 * @describe 斐波那契数列
 */
public class FibonacciSequence {

    public static void main(String[] args) {
        System.out.println(test1(3));
        System.out.println(test2(5));
        System.out.println(test2(6));
        System.out.println(Arrays.toString(test3(5)));
        System.out.println(Arrays.toString(test3(6)));
    }

    /**
     * 递归方式实现(数值较大时时间较长)
     *
     * @param i 第几位
     * @return 参数
     */
    private static int test1(int i) {
        int one = 1, two = 2;
        if (i == one || i == two) {
            return one;
        } else {
            return test1(i - 1) + test1(i - 2);
        }
    }

    /**
     * 累加获取参数
     *
     * @param i 第几位
     * @return 数据
     */
    private static int test2(int i) {
        int a = 1, b = 1, c = 0;
        for (int j = 3; j <= i; j++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }

    /**
     * 以数组方式实现
     *
     * @param i 第几位
     * @return 参数
     */
    private static int[] test3(int i) {
        int[] list = new int[i];
        list[0] = list[1] = 1;
        for (int j = 2; j < i; j++) {
            list[j] = list[j - 1] + list[j - 2];
        }
        return list;
    }
}
